package org.mx.service.server;

import org.mx.StringUtils;
import org.mx.TypeUtils;
import org.springframework.beans.factory.annotation.Value;

import lombok.Getter;
import lombok.Setter;

/**
 * 描述： RESTful服务配置对象
 *
 * @author john peng
 * Date time 2018/7/18 下午1:17
 */
public class RestfulServerConfigBean extends HttpServerConfigBean {
	@Value("${restful.enabled:false}")
	@Getter
	private boolean enabled;
	@Value("${restful.host:0.0.0.0}")
	@Getter
	@Setter
	private String host;
	@Value("${restful.port:9999}")
	@Getter
	@Setter
	private int port;
	@Value("${restful.threads:100}")
	@Getter
	private int threads;
	@Value("${restful.security:false}")
	@Getter
	private boolean security;
	@Value("${restful.security.keystore:}")
	@Getter
	private String keystorePath;
	@Value("${restful.security.keystorePassword:}")
	@Getter
	private String keystorePassword;
	@Value("${restful.security.keyManagerPassword:}")
	@Getter
	private String keyManagerPassword;
	@Value("${restful.security.keystoreType:JKS}")
	@Getter
	private String keystoreType;
	@Value("${restful.security.keyAlias:jetty}")
	@Getter
	private String keyAlias;
	@Value("${restful.security.keyManagerFactoryAlgorithm:SunX509}")
	@Getter
	private String keyManagerFactoryAlgorithm;
	@Value("${restful.security.maxCertPathLength:-1}")
	@Getter
	private int maxCertPathLength;
	@Value("${restful.security.validatCerts:true}")
	@Getter
	private boolean validateCerts;
	@Value("${restful.security.validatePeerCerts:false}")
	@Getter
	private boolean validatePeerCerts;
	@Value("${restful.security.needClientAuth:false}")
	@Getter
	private boolean needClientAuth;
	@Value("${restful.security.wantClientAuth:false}")
	@Getter
	private boolean wantClientAuth;

	@Value("${restful.idleTimeoutSecs:300}")
	@Getter
	private long idleTimeoutSecs;
	@Value("${restful.outputSize:32K}")
	private String outputSizeString;
	@Value("${restful.requestHeaderSize:8K}")
	private String requestHeaderSizeString;
	@Value("${restful.responseHeaderSize:8K}")
	private String responseHeaderSizeString;

	@Value("${restful.security.cipherSuites:TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA384,TLS_DHE_RSA_WITH_AES_128_GCM_SHA384,TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA256,TLS_DHE_RSA_WITH_AES_256_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,TLS_DHE_RSA_WITH_AES_256_GCM_SHA384}")
	private String cipherSuitesString;
	@Value("${restful.security.protocols:TLSv1.2}")
	private String protocolsString;
	@Value("${restful.service.classes:}")
	private String serviceClasses;
	@Value("${restful.service.base.packages:}")
	private String resourceBasePackages;
	@Value("${restful.register.classes:}")
	private String registerClasses;

	/**
	 * 默认的构造函数
	 */
	public RestfulServerConfigBean() {
		super(ServerType.RESTful);
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see HttpServerConfigBean#getCipherSuites()
	 */
	public String[] getCipherSuites() {
		return StringUtils.split(cipherSuitesString, ",", true, true);
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see HttpServerConfigBean#getProtocols()
	 */
	public String[] getProtocols() {
		return StringUtils.split(protocolsString, ",", true, true);
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see HttpServerConfigBean#getOutputSize()
	 */
	public long getOutputSize() {
		return TypeUtils.string2Size(outputSizeString, 32 * 1024);
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see HttpServerConfigBean#getRequestHeaderSize()
	 */
	public long getRequestHeaderSize() {
		return TypeUtils.string2Size(requestHeaderSizeString, 8 * 1024);
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see HttpServerConfigBean#getResponseHeaderSize()
	 */
	public long getResponseHeaderSize() {
		return TypeUtils.string2Size(responseHeaderSizeString, 8 * 1024);
	}

	/**
	 * 获取配置的RESTful服务类列表
	 *
	 * @return RESTful服务类类别
	 */
	public String[] getServiceClasses() {
		return StringUtils.isBlank(serviceClasses) ? null : StringUtils.split(serviceClasses);
	}

	/**
	 * 获取配置的RESTful服务资源基准包路径，如果有多个包，用逗号分隔。系统将自动扫描。
	 *
	 * @return RESTful服务资源基本包路径
	 */
	public String[] getResourceBasePackages() {
		String packages = StringUtils.isBlank(resourceBasePackages) ? super.getSpringBootClassPackage()
				: resourceBasePackages;
		return StringUtils.split(packages);
	}

	/**
	 * 获取配置的自定义资源（CustomRegister）注册类名列表
	 * 
	 * @return 配置的自定义资源注册类名列表
	 */
	public String[] getRegisterClasses() {
		return StringUtils.split(registerClasses);
	}
}
